package quake.wink.service;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import quake.responses.Tweets;
import quake.wink.WinkService;

@Path("/tweet")
public class Tweet extends WinkService {
    private static final Log log = LogFactory.getLog(Tweet.class);

    private static final String PARAM_HASH_TAGS = "hashTags";
    private static final String PARAM_USER = "user";
    private static final String PARAM_HOUR = "hour";
    private static final String PARAM_WORDS = "words";

    private static final String SEPARATOR = ",";

    private static final String HASH_TAGS_S = "#";

    @Path("/lasthour")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public Tweets listLastHour() {
        Tweets t = new Tweets();
        try {
            t.setTweets(tweetDao.listLastHour());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e);
        }

        return t;
    }

    @Path("/hashtags/{" + PARAM_HASH_TAGS + "}")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public Tweets listHashTags(@PathParam(PARAM_HASH_TAGS) String hashTags) {
        Tweets t = new Tweets();
        try {
            final String h = HASH_TAGS_S + hashTags;
            t.setTweets(tweetDao.listHashTag(h));
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e);
        }

        return t;
    }

    @Path("/user/{" + PARAM_USER + "}")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public Tweets listUser(@PathParam(PARAM_USER) String user) {
        Tweets t = new Tweets();
        try {
            t.setTweets(tweetDao.listUser(user));
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e);
        }

        return t;
    }

    @Path("/freesearch/and/{" + PARAM_HOUR + "}/{" + PARAM_WORDS + "}")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public Tweets listAndFreeSearch(@PathParam(PARAM_HOUR) String hour,
                                    @PathParam(PARAM_WORDS) String words) {
        int h;
        try {
            h = Integer.valueOf(hour);
        } catch (NumberFormatException e) {
            return null;
        }

        if (words == null || words.isEmpty()) {
            return null;
        }

        List<String> l = new ArrayList<String>();
        String[] ws = words.split(SEPARATOR);
        for (String s : ws) {
            if (!s.isEmpty()) {
                l.add(s);
            }
        }

        Tweets t = new Tweets();
        try {
            t.setTweets(tweetDao.listAndFreeSearch(h, l));
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e);
        }

        return t;
    }

    @Path("/freesearch/or/{" + PARAM_HOUR + "}/{" + PARAM_WORDS + "}")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public Tweets listOrFreeSearch(@PathParam(PARAM_HOUR) String hour,
                                   @PathParam(PARAM_WORDS) String words) {
        int h;
        try {
            h = Integer.valueOf(hour);
        } catch (NumberFormatException e) {
            return null;
        }

        if (words == null || words.isEmpty()) {
            return null;
        }

        List<String> l = new ArrayList<String>();
        String[] ws = words.split(SEPARATOR);
        for (String s : ws) {
            if (!s.isEmpty()) {
                l.add(s);
            }
        }

        Tweets t = new Tweets();
        try {
            t.setTweets(tweetDao.listOrFreeSearch(h, l));
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e);
        }

        return t;
    }
}
